package io.awen.modules.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.awen.common.exception.RRException;
import io.awen.common.utils.PageUtils;
import io.awen.common.utils.Query;
import io.awen.modules.sign.dao.SignSinkDao;
import io.awen.modules.sign.entity.SignContainerEntity;
import io.awen.modules.sign.entity.SignSinkEntity;
//import io.awen.modules.sign.entity.SignSinkRealtimeLiquidLevelEntity;
import io.awen.modules.sign.entity.SignFleetEntity;
import io.awen.modules.sign.entity.SignUnitEntity;
import io.awen.modules.sign.qo.SignSinkQo;
import io.awen.modules.sign.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;


@Service("signSinkService")
public class SignSinkServiceImpl extends ServiceImpl<SignSinkDao, SignSinkEntity> implements SignSinkService {

    @Autowired
    private SignSinkService signSinkService;
    @Autowired
    private SignSinkDao signSinkDao;
//    @Autowired
//    private SignSinkRealtimeLiquidLevelService signSinkRealtimeLiquidLevelService;

    private HashMap<String, SignSinkEntity> sensorSinkMap;
    private ScheduledExecutorService scheduledExecutorService;

    @Override
    public Map<String, SignSinkEntity> getSensorSinkMap() {
        return sensorSinkMap;
    }

    @Override
    public SignSinkEntity getBySensorId(String sensorId) {
        if(StringUtils.isBlank(sensorId)){
            throw new RRException("sensorId不能为空");
        }
        return signSinkDao.getBySensorId(sensorId);
    }

//    @Autowired
//    private SignSinkAlarmService signSinkAlarmService;

    @Autowired
    private SignUnitService signUnitService;

    @Autowired
    private SignFleetService signFleetService;

    @PostConstruct
    private void init() {
        sensorSinkMap = new HashMap<>(400);

        List<SignSinkEntity> list = list(
                new QueryWrapper<SignSinkEntity>().lambda()
        );
        for (SignSinkEntity entity : list) {
            // 获取容器当前液位计监测值
            entity.setSensorValue(entity.getRealtimeLiquidLevel());
            entity.setDataTime(new Date());
            sensorSinkMap.put(entity.getSensorId(), entity);
        }

        // 设置定时器，不断刷新容器中液位计的监测值
//        scheduledExecutorService = new ScheduledExecutorService();
    }

    @Override
    public SignSinkEntity findSink(String sensorId) {
        return sensorSinkMap.get(sensorId);
    }

    @Override
    public SignSinkEntity findSink(Long id) {
        Iterator<SignSinkEntity> iterator = sensorSinkMap.values().iterator();
        while (iterator.hasNext()) {
            SignSinkEntity Sink = iterator.next();
            if (Sink.getId().equals(id)) {
                return Sink;
            }
        }
        return null;
    }


    @Override
    public SignSinkEntity findAllSink(Long id) {

        return signSinkService.getById(id);
    }


    @Override
    public void updateSensorValue(String sensorId, Double value, Date dataTime) {

    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void updateSensorValue(String sensorId, Double value, Date dataTime) {
//        SignSinkEntity Sink = sensorSinkMap.get(sensorId);
//        Sink.setSensorValue(value);
//        if (dataTime == null) {
//            dataTime = new Date();
//        }
//        Sink.setDataTime(dataTime);
//
//
//        //更新最新液位
//        SignSinkEntity entity = this.getBySensorId(sensorId);
//        if(entity!=null){
//            entity.setRealtimeLiquidLevel(value);
//            signSinkDao.updateById(entity);
//        }
////        sensorSinkMap.put(sensorId,Sink);
//        signSinkAlarmService.juegeAlarm(Sink);
//
//
//        //同步到数据库sign_Sink_realtime_liquid_level
//        SignSinkRealtimeLiquidLevelEntity levelEntity = new SignSinkRealtimeLiquidLevelEntity();
//        levelEntity.setSinkId(Sink.getId());
//        levelEntity.setSensorId(Sink.getSensorId());
//        levelEntity.setRealtimeLiquidLevel(value);
//        levelEntity.setName(Sink.getName());
//        signSinkRealtimeLiquidLevelService.insert(levelEntity);
//
//    }



    @Override
    public PageUtils queryPage(SignSinkQo qo) {
        IPage<SignSinkEntity> page = this.page(
                new Query<SignSinkEntity>().getPage(qo),
                new QueryWrapper<SignSinkEntity>().lambda()
                        .eq(StringUtils.isNotBlank(qo.getStatus()), SignSinkEntity::getStatus, qo.getStatus())
                        .ge(qo.getStartTime() != null, SignSinkEntity::getCreateTime, qo.getStartTime())
                        .le(qo.getEndTime() != null, SignSinkEntity::getCreateTime, qo.getEndTime())
                        .like(StringUtils.isNotBlank(qo.getName()), SignSinkEntity::getName, qo.getName())
                        .like(StringUtils.isNotBlank(qo.getJiedao()), SignSinkEntity::getJiedao, qo.getJiedao())
                        .like(StringUtils.isNotBlank(qo.getSheqv()), SignSinkEntity::getSheqv, qo.getSheqv())
                        .orderByDesc(SignSinkEntity::getId)
        );



        return new PageUtils(page);
    }

    @Override
    public void saveAndCache(SignSinkEntity signSink) {
        save(signSink);
        sensorSinkMap.put(signSink.getSensorId(), signSink);
    }

    @Override
    public void updateAndCache(SignSinkEntity signSink) {
        updateById(signSink);
        SignSinkEntity byId = getById(signSink.getId());
        byId.setSensorValue(sensorSinkMap.get(byId.getSensorId()).getSensorValue());
        sensorSinkMap.put(byId.getSensorId(), byId);
    }

    @Override
    public int total(SignSinkQo qo) {
        int count = this.count(
                new QueryWrapper<SignSinkEntity>().lambda()
                        .ge(qo.getStartTime() != null, SignSinkEntity::getCreateTime, qo.getStartTime())
                        .le(qo.getEndTime() != null, SignSinkEntity::getCreateTime, qo.getEndTime())
        );
        return count;
    }

}